<template>
  <div class="app-container">
    <el-card>
      <!--用户数据-->
      <el-form :model="queryParams" ref="queryForm" size="small" label-width="50px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="年级">
              <el-input v-model.trim="queryParams.name" placeholder="输入内容" clearable @keyup.enter.native="handleQuery"
                @input="getList" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['project:class:add']">新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="tableList" height="440">
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="年级" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="handleUpdate(scope.row)" v-hasPermi="['project:class:edit']">编辑</el-button> -->
            <el-button style="color: #f56c6c;" type="text" @click="handleDelete(scope.row)"
              v-hasPermi="['project:class:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </el-card>
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="30%">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="年级: " prop="name">
              <!-- <el-input v-model.trim="form.name" placeholder="输入内容" clearable /> -->
              <el-select v-model="form.name" placeholder="请选择年级" clearable filterable style="width:100%">
                <el-option v-for="item in integralList" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDiction, savaDiction, putDiction, deleteDiction } from "@/api/dataDictionary";
export default {
  name: "dataDictionary",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 表单参数
      form: {},
      integralList: [2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请选择年级", trigger: "change" },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listDiction({ ...this.queryParams }).then(res => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleAdd() {
      this.title = "新增年级";
      this.resetForm("queryForm");
      this.form = {};
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "编辑年级";
      this.resetForm("queryForm");
      this.form = { ...row };
      this.open = true;
    },
    cancel() {
      this.open = false;
      this.form = {};
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryForm");
      this.getList();
    },
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除"' + row.name + '"数据项？')
        .then(function () {
          return deleteDiction(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      const urlType = this.form.id >= 0 ? putDiction : savaDiction;
      const msgType = this.form.id >= 0 ? "编辑成功" : "新增成功";
      this.$refs["form"].validate((valid) => {
        if (valid) {
          urlType({ ...this.form }).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(msgType);
              this.form = {};
              this.getList();
              this.open = false;
            }
          })
        }
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
